package com.yanqu.road.server.cross.service;

import com.google.protobuf.AbstractMessage;
import com.google.protobuf.InvalidProtocolBufferException;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.syncMessage.SyncMessage;
import com.yanqu.road.pb.syncMessage.SyncUnion;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.server.command.ICrossServiceCommand;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.concurrent.CompletableFuture;


public class CrossGetUserActivityRankServiceCmd implements ICrossServiceCommand {
    @Override
    public CompletableFuture<AbstractMessage.Builder<?>> execute(SyncMessage.Request request) throws InvalidProtocolBufferException {
        SyncMessage.ActivityRankListReqMsg reqMsg = SyncMessage.ActivityRankListReqMsg.parseFrom(request.getDataBytes());
        int activityId = reqMsg.getActivityId();
        int conditionType = reqMsg.getConditionType();
        long serverId = reqMsg.getServerId();
        long rankNum = reqMsg.getRankNum();

        long myUserId = reqMsg.getMyUserId();
        SyncMessage.ActivityRankListRespMsg.Builder respMsg = SyncMessage.ActivityRankListRespMsg.newBuilder();
        CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(activityId, conditionType);
        if (rankListModel != null) {
            RankProto.RankListMsg.Builder rankListMsg = rankListModel.getRankListMsg();
            int rank = 0;
            for (RankProto.RankTempMsg msg : rankListMsg.getRankListList()) {
                rank++;
                if (serverId != 0 && serverId != msg.getPlayerBaseData().getServerId()) {
                    continue;
                }
                if (rank <= rankNum) {
                    SyncMessage.RankTempMsg.Builder tempMsg = SyncMessage.RankTempMsg.newBuilder();
                    tempMsg.setUserId(msg.getUserId());
                    tempMsg.setPlayerBaseData(msg.getPlayerBaseData());
                    tempMsg.setValue(msg.getValue());
                    tempMsg.setParam(msg.getParam());
                    tempMsg.setRank(rank);
                    respMsg.addRankList(tempMsg);
                } else {
                    if (myUserId == 0) {
                        break;
                    }
                }

                if (msg.getUserId() == myUserId) {
                    respMsg.setMyRank(rank);
                }
            }
        }
        CompletableFuture<AbstractMessage.Builder<?>> completableFuture = new CompletableFuture<>();
        completableFuture.complete(respMsg);
        return completableFuture;
    }
}
